package com.kaisir.demo.flow.controller;

import com.kaisir.demo.flow.bean.SnakerEngineFacets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snaker.engine.access.QueryFilter;
import org.snaker.engine.entity.Process;
import org.snaker.engine.entity.WorkItem;
import org.snaker.engine.model.TaskModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@SessionAttributes({"app", "user"}) //好玩
public class IndexController {
    private static Logger logger = LoggerFactory.getLogger("IndexController");

    @Autowired
    SnakerEngineFacets engineFacets;

    @RequestMapping("index")
    public String index(Model model, String appId, String username) {
        //模拟登录
        appId = appId == null ? "applyApp" : appId;
        username = username == null ? "admin" : username;
        model.addAttribute("app", appId);
        model.addAttribute("user", username);
        //获取当前用户的待办
        List<WorkItem> todoList = engineFacets.getEngine().query().getWorkItems(null, new QueryFilter().setAppId(appId).setOperator(username));
        model.addAttribute("todoList", todoList);
        //获取所有流程定义列表
        //List<String> processList = engineFacets.getAllProcessNames();
        List<Process> processList = engineFacets.getEngine().process().getProcesss(new QueryFilter().setAppId(appId));
        model.addAttribute("processList", processList);
        return "index";
    }

    @RequestMapping("deploy")
    public String deploy() {
        //
        engineFacets.initFlows();
        return "redirect:index";
    }

    @RequestMapping("newFlow")
    public String newFlow(String processId) {
        Process process = engineFacets.getEngine().process().getProcessById(processId);
        return "redirect:" + process.getInstanceUrl().concat(processId);
    }

    @RequestMapping(value = "doFlow", method = RequestMethod.GET)
    public String doFlowGet(Model model, String taskId) {
        TaskModel taskModel = engineFacets.getEngine().task().getTaskModel(taskId);
        logger.info("Task : {}", taskModel);
        model.addAttribute("formUrl", taskModel.getForm());
        model.addAttribute("taskId", taskId);
        return "doFlow";
    }

    @RequestMapping(value = "doFlow", method = RequestMethod.POST)
    public String doFlowPost(@ModelAttribute("user") String user, String taskId, int choose, String comment) {
        logger.info("User='{}' 执行审核：taskId:{},{},{}", user, taskId, choose, comment);
        Map<String, Object> attrs = new HashMap<String, Object>();
        attrs.put("comment", comment);
        switch (choose) {
            case 0: { //拒绝
                engineFacets.executeAndJump(taskId, user, attrs, null);
            }

            case 1: { //通过
                engineFacets.execute(taskId, user, attrs);
            }
        }
        return "redirect:index";
    }


}
